# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "Header.h"
# include "DirectoryOps.h"
# include "NArrayTree.h"

char **searchFile(char *name, char *outputFile, struct VFS *header) {
	int i, j, l = 0;	
	char *path;
	char fn[100];
	char **list;
	list = (char **) malloc(sizeof(char *) * MAX_FD_SIZE);
	path = (char *) malloc (sizeof(char) * 100);
	for(i = 0; i < MAX_FD_SIZE; i++) {
		if(header -> freeList[i] == 1) {
			copyString(path, header -> fd[i].filePath);
			copyString(fn, split(path));
			while(*fn) {
				j = compareString(name, fn);
				if(j) {
					*(list + l)= (char *) malloc(sizeof(char) * 100);
					copyString (*(list + l), header -> fd[i].filePath);
					l++;
					break;
				}
				path = truncateLeft(path, stringLength(fn));
				copyString(fn, split(path));
			}
		}
	}
	*(list + l)= (char *) malloc(sizeof(char) * 100);
	copyString (*(list + l), " ");
	return list;
}

int compareString(char *DirName, char *filePath){ 
	int i = 0;
	int l = stringLength(DirName);
	int m = stringLength(filePath);
	if(l == 0 || l > m)
		return 0;
	while(i < l) {
		if( *(DirName + i) == *(filePath + i))
			i++;     	
		else 
			return 0;
	}
	return 1;
}
